package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Organization;
import cn.wolfcode.crm.mapper.OrganizationMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IOrganizationService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class OrganizationServiceImpl implements IOrganizationService {
    @Autowired
    private OrganizationMapper organizationMapper;

    public void saveOrUpdate(Organization entity) {
        if (entity.getId() == null) {
            organizationMapper.insert(entity);
        } else {
            organizationMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        organizationMapper.deleteByPrimaryKey(id);
    }

    public Organization get(Long id) {
        return organizationMapper.selectByPrimaryKey(id);
    }

    public List<Organization> listAll() {
        return organizationMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = organizationMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<Organization> organizations = organizationMapper.queryList(qo);
        return new PageResult(count, organizations);
    }
    public void changeCooperation(Long id) {
        organizationMapper.changeCooperation(id);
    }

    @Override
    public List<Map<String, Object>> selectOrganizationInfo() {
        return organizationMapper.selectOrganizationInfo();
    }
}
